package com.woniuxy.cloud.carl.marketing.app.apiImpl;

import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.marketing.client.CouponClient;
import com.woniuxy.cloud.carl.marketing.client.param.AddUserCouponParam;
import com.woniuxy.cloud.carl.marketing.client.param.UseCouponParam;
import com.woniuxy.cloud.carl.marketing.client.result.CouponDto;
import com.woniuxy.cloud.carl.marketing.common.StatusEnum;
import com.woniuxy.cloud.carl.marketing.infra.model.*;
import com.woniuxy.cloud.carl.marketing.service.*;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.CommonResultCodeEnum;
import com.woniuxy.result.Result;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class CouponAPIImpl implements CouponClient {
    @Autowired
    CouponService couponService;
    @Autowired
    ServiceCouponService serviceCouponService;
    @Autowired
    StoreCouponService storeCouponService;
    @Autowired
    DeductionService deductionService;
    @Autowired
    FullDiscountCouponService fullDiscountCouponService;
    @Override
    @Log("根据用户Id查询优惠券")
    public Result<List<CouponDto>> getCouponByUserId(Long userId) {

        return couponService.getCouponByUserId(userId);
    }

    @Override
    @Log("用户领取优惠券")
    public Result addUserCoupon(AddUserCouponParam param) {

        return couponService.addUserCoupon(param);
    }
    @Override
    @Log("用户查询自己的服务券")
    public Result<List<ServiceCoupon>> getMyServiceCoupon(Long serviceId, Long userId) {
        return couponService.getMyServiceCoupon(serviceId,userId);
    }

    @Override
    @Log("用户查询自己的满减券")
    public Result<List<FullDiscountCoupon>> getMyDiscount(Long userId) {
        return couponService.getMyDiscount(userId);
    }

    @Override
    @Log("用户查询自己的抵扣券")
    public Result<List<Deduction>> getMyCouponBySpuId(Long spuId, Long userId) {
        Result<List<Deduction>> myCouponBySpuId = couponService.getMyCouponBySpuId(spuId, userId);
        return myCouponBySpuId;
    }

    @Override
    @Log("用户使用优惠券")
    @GlobalTransactional
    public Result<Coupon> useCoupon(UseCouponParam param) {
        Result<Coupon> couponResult = couponService.useCoupon(param.getUserId(), param.getCouponId());
        Coupon data = couponResult.getData();
        switch (data.getCouponType().intValue()){
            case (1):{
                deductionService.useDeduction(param.getSkuId(),data.getCouponNum());
                break;
            }
            case (2):{
                fullDiscountCouponService.useFullDiscount(param.getPrice(),data.getCouponNum());
                break;
            }
            case (3):{
                serviceCouponService.useServiceCoupon(param.getStoreId(), data.getCouponNum());
                break;
            }
            default:{
                break;
            }
        }
        return Result.success();
    }
}
